package larc.dao.impl;

import java.util.List;

import org.hibernate.Session;

import larc.dao.intr.PostulanteDao;
import larc.model.Postulante;
import larc.util.HibernateUtil;

public class PostulanteDaoImpl implements PostulanteDao{

	
	public void insertar(Postulante postulante) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try{
		    session.beginTransaction();
		    session.persist(postulante);
		    session.getTransaction().commit();		   
		}catch(Exception e)
		{
			System.out.println("Error al insertar postulante : " + e.getMessage());
			session.beginTransaction().rollback();
		}finally{
			   if (session != null && session.isOpen()) 
			   {
				   session.close();
			   }
		}
	}

	public void actualizar(Postulante postulante) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try{
			session.beginTransaction();
			session.update(postulante);
			session.beginTransaction().commit();
		}catch(Exception e)
		{
			System.out.println("Error al actualizar : " + e.getMessage());
			session.beginTransaction().rollback();
		}finally{
			   if (session != null && session.isOpen()) 
			   {
				   session.close();
			   }
		}
	}

	public void eliminar(Postulante postulante) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try{
			session.beginTransaction();
			session.delete(postulante);
			session.beginTransaction().commit();
		}catch(Exception e)
		{
			System.out.println("Error al eliminar : " + e.getMessage());
			session.beginTransaction().rollback();
		}finally{
			   if (session != null && session.isOpen()) 
			   {
				   session.close();
			   }
		}	
		
	}

	@SuppressWarnings("unchecked")
	public List<Postulante> listarTodo() {
		Session session = HibernateUtil.getSessionFactory().openSession();	
		try{
			return session.createQuery("from Postulante p order by p.persona.peApellidoP").list();
		}catch(Exception e)
		{
			System.out.println("Error al cargar : " + e.getMessage());
			
		}finally{
		   if (session != null && session.isOpen()) 
		   {
			   session.close();
		   }
		}
		return null;
	}

}
